<?php
require('db.php');

$vak = $_GET['vak'];
$parent = $_GET['parent'];
$beoordelaar = $_GET['beoordelaar'];
$beoordeelde = $_GET['beoordeelde'];

$result = mysql_query("
SELECT c.id, c.omschrijving
FROM competenties c
JOIN (
	SELECT cpv.id, cpv.vakID, cpv.competentieID
       	FROM competentiespervak cpv
        LEFT JOIN (
        	SELECT competentiePerVakID
                FROM behaaldecompetenties
                WHERE beoordelaarID='$beoordelaar' AND beoordeeldeID='$beoordeelde')x
	ON (cpv.id=x.competentiePerVakID)
        WHERE x.competentiePerVakID IS NULL AND cpv.vakID=(SELECT id FROM vak WHERE naam='$vak')) y
ON (c.id=y.competentieID)
WHERE c.parentID='$parent'
") or die (mysql_error());

$data = array();

while ($row = mysql_fetch_object($result)) {
    $data [] = $row;
}

echo json_encode($data);
?>